package com;

import org.eclipse.jetty.server.ConnectionFactory;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.servlet.My;

import hyl.core.MyFun;
import hyl.core.conf.MyConst;
import hyl.core.io.MyPath;

/**
 * jetty 服务器
 */
public class App {

	private static Logger logger = LoggerFactory.getLogger(App.class);
	private static String HTTP_IP = "0.0.0.0";
	private static int HTTP_PORT = 80;
	private static int HTTPS_PORT = 8443;
	private static String CONTEXT = "/";

	public static void main(String[] args) {
		My.getInstance();
		MyConst sysdict = MyConst.getInstance();
		HTTP_IP = sysdict.get("IP");
		HTTP_PORT = MyFun.str2int(sysdict.get("HTTPPORT"));
		HTTPS_PORT = MyFun.str2int(sysdict.get("SSLPORT"));
		CONTEXT = sysdict.get("CONTEXT");
		Server server = new App().createServer();
		try {
			server.start();
			System.out.println("jetty服务开启！！！");
			server.join();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private App() {
	}

	private Server createServer() {
		// jetty server 默认的最小连接线程是8，最大是200，连接线程最大闲置时间60秒
		Server server = new Server();

		HttpConfiguration httpConfig = new HttpConfiguration();
		if (HTTPS_PORT > 0) {
			httpConfig.setSecureScheme("https");
			httpConfig.setSecurePort(HTTPS_PORT);
		}
		httpConfig.setOutputBufferSize(32768);
		httpConfig.setRequestHeaderSize(8192);
		httpConfig.setResponseHeaderSize(8192);
		httpConfig.setSendServerVersion(true);
		httpConfig.setSendDateHeader(false);
		httpConfig.setHeaderCacheSize(512);
		ConnectionFactory connectionFactory = new HttpConnectionFactory(httpConfig);
		ServerConnector connector = new ServerConnector(server, connectionFactory);
		connector.setPort(HTTP_PORT);
		connector.setSoLingerTime(-1);
		// 连接线程最大空闲时间
		connector.setIdleTimeout(30000);
		server.addConnector(connector);
		String path = 	MyPath.getPathOfPro();
		System.out.println(path);
		//URL url = App.class.getProtectionDomain().getCodeSource().getLocation();
//		String path = "";
//		try {
//			path = new File(url.toURI()).getParent();
//		} catch (URISyntaxException e) {
//		}
		// 如果在springMVC配置文件中，引入了springIOC配置文件applicationContext.xml,在这里就没有必要再创建applicationContext并将其放入到webApplicationContext中
		// 这里使用ClassPathXmlApplicationContext的原因是在打包后，打包的jar文件目录其实是classpath目录，不会出现文件找不到
		WebAppContext webAppContext = new WebAppContext();
		/*
		 * contextPath 是URL的前缀。例如一个contextPath 是/foo，它将处理 /foo， /foo/index.html，
		 * /foo/bar/，and /foo/bar/image.png等请求， 但是它不会处理像/，/other/，or /favicon.ico这样的请求
		 */
		webAppContext.setContextPath(CONTEXT);
		webAppContext.setDescriptor(path+"webapp/WEB-INF/web.xml");

		// 配置资源，这个配置是一个目录，包含各种静态资源信息，可以是图片或者HTML页面。
		webAppContext.setResourceBase( path+"webapp");
		// 配置监听主机ip或名称，没有配置的将不会被监听到。
		// webAppContext.setVirtualHosts(new String[] {});
		webAppContext.setConfigurationDiscovered(true);
		webAppContext.setParentLoaderPriority(true);
		webAppContext.setMaxFormContentSize(10 << 20);
		webAppContext.setClassLoader(Thread.currentThread().getContextClassLoader());

		// 配置jetty支持jsp,jstl=====end

		server.setHandler(webAppContext);
		return server;

	}
}
// new ClassPathXmlApplicationContext(new
// String[]{"spring.xml","springbean.xml"});

// 或者用通配符:
// ApplicationContext ac = new
// ClassPathXmlApplicationContext("classpath:/*.xml");
// 用classpath路径
// ApplicationContext factory = new
// ClassPathXmlApplicationContext("classpath:appcontext.xml");
// ApplicationContext factory = new
// ClassPathXmlApplicationContext("appcontext.xml");

// ClassPathXmlApplicationContext使用了file前缀是可以使用绝对路径的
// ApplicationContext factory = new
// ClassPathXmlApplicationContext("file:F:/workspace/example/src/appcontext.xml");

// 用文件系统的路径,默认指项目的根路径
// ApplicationContext factory = new
// FileSystemXmlApplicationContext("src/appcontext.xml");
// ApplicationContext factory = new
// FileSystemXmlApplicationContext("webRoot/WEB-INF/appcontext.xml");

// 使用了classpath:前缀,这样,FileSystemXmlApplicationContext也能够读取classpath下的相对路径
// ApplicationContext factory = new
// FileSystemXmlApplicationContext("classpath:appcontext.xml");
// ApplicationContext factory = new
// FileSystemXmlApplicationContext("file:F:/workspace/example/src/appcontext.xml");

// 不加file前缀
// ApplicationContext factory = new
// FileSystemXmlApplicationContext("F:/workspace/example/src/appcontext.xml");
// IHelloWorld hw = (IHelloWorld)factory.getBean("helloworldbean");
